using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Data;
using pos.entity;
using System.Collections;
using NHibernate.Criterion;
namespace idea.tools
{
    class DealerComboxTool
    {
        private ComboBox cboList;
        private Hashtable m_hashTable;

        public bool active
        {
            get
            {
                if (cboList == null)
                    return false;
                return cboList.Visible;
            }
           
        }
        private Control txtCmd;
        public delegate void _OnClose();
        public _OnClose onClose;
        public DealerComboxTool(Form parent, Control position, _OnClose onclose)
        {
            m_hashTable = new Hashtable();
            t_dealer d = new t_dealer();
            //DataSet set = d.query("", "order by companyName");
            IList list = d.List(d.DC.AddOrder(Order.Asc("companyName")));
           
            foreach (Dealer r in list)
            {
                d.Init(r);
                string key=Spell.MakeSpellCode(d.companyName,SpellOptions.FirstLetterOnly);
                while (m_hashTable[key] != null)
                       key += "_";
                m_hashTable.Add(key, d.companyName);
                    
            }
            if (cboList == null)
            {
                cboList = new ComboBox();

                cboList.AutoSize = true;
                cboList.Font = position.Font;
                cboList.Location = position.Location;
                cboList.DropDownHeight = parent.Height-100;

                cboList.Name = "cboDealerList";
                cboList.Size = position.Size;
                cboList.Width = 700;
                cboList.TabIndex = 0;
                

                parent.Controls.Add(cboList);
                txtCmd = position;
                onClose = onclose;
                
            }

            cboList.SelectionChangeCommitted += cboList_SelectionChangeCommitted;
            cboList.DropDownClosed += cboList_DropDownClosed;
            cboList.KeyDown += cboList_KeyDown;
            
        }
        public  void showList(string key)
        {
            cboList.Items.Clear();
            cboList.Visible = true;

            
            cboList.DisplayMember = "Name";
            cboList.ValueMember = "Value";

            foreach (Object objs in m_hashTable.Keys)
            {
                string obj = (string)objs;
                if (obj.IndexOf(key) >= 0)
                {
                    TNameValue vo1 = new TNameValue();
                    vo1.Name = (string)m_hashTable[obj];//string.Format("{0,-15}{2}", t.commodityID,t.retailPrice,t.commodityName);
                    vo1.Value = (string)m_hashTable[obj];
                    cboList.Items.Add(vo1);
                }
            }
            TNameValue vo = new TNameValue();
            vo.Name = "";
            vo.Value = "";
            cboList.Items.Add(vo);
            cboList.Focus();
            cboList.DroppedDown = true;
          
        }
        private void cboList_DropDownClosed(object sender, EventArgs e)
        {
            cboList.Visible = false;
        }
        private void cboList_SelectionChangeCommitted(object sender, EventArgs e)
        {
          
            if (cboList.SelectedItem == null)
                return;
            TNameValue t = (TNameValue)cboList.SelectedItem;
            txtCmd.Text = t.Value;
            //  RowAddOne();
            onClose();
        }
        private void cboList_KeyDown(object sender, KeyEventArgs e)
        {

            switch (e.KeyCode)
            {
                case Keys.Enter:
                    cboList.Visible = false;
                    if (cboList.SelectedItem == null)
                        return;
                    TNameValue t = (TNameValue)cboList.SelectedItem;
                    txtCmd.Text = t.Value;
                    //  RowAddOne();
                    onClose();
                    break;
                case Keys.Escape:
                    txtCmd.Text = "";
                    break;
             
            }
            
            
           
            
        }
    }
}
